package hrc.heart_rate_monitor;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class HeartRateView extends View {

	private Bitmap mBitmap;
	private Paint mPaint = new Paint();
	private Canvas mCanvas = new Canvas();
	private Paint linePaint=new Paint();
	
	private float mLastY;

	private float mXOffset;
	private float mMaxY;
	private float mWidth;
	private float mHeight;
	
	private float mLastValue;
	private float avg=70;
	private int avgColor=200;
	
	
	private CanvasFilledListener listener;
	private long startTime;
	private long endTime;
	private long avgDuration;
	

	

	public HeartRateView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
		linePaint.setStrokeWidth(3);
		linePaint.setColor(Color.RED);
	
	}
	
	
	

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
		mCanvas.setBitmap(mBitmap);
		mCanvas.drawColor(0xFFFFFFFF);
		mXOffset = w * 0.5f;
		
		mWidth = w;
		mHeight = h;
		if (mWidth < mHeight) {
			mMaxY = h;
		} else {
			mMaxY = h - 50;
		}
		mLastY = mMaxY;
		mLastValue =(200-avgColor)*5+mXOffset;;
		startTime=System.currentTimeMillis();
		super.onSizeChanged(w, h, oldw, oldh);
	}
	
	public void updateRateForDraw(int value){
		final Canvas canvas = mCanvas;	
		float v=(value-avgColor)*5+mXOffset;
		float y=mLastY+3;
		canvas.drawLine(mLastValue, mLastY,v, y,  linePaint);
		mLastY=y;
		mLastValue=v;
	}
	
	
	 @Override
     protected void onDraw(Canvas canvas) {
         synchronized (this) {
             if (mBitmap != null) {
                 final Paint paint = mPaint;
                 if (mLastY >= mMaxY) {
                	 endTime=System.currentTimeMillis();
                     avgDuration=(endTime-startTime)/1000;
                     startTime=endTime;
                     mLastY = 0;
                     final Canvas cavas = mCanvas;
                     final float xoffset = mXOffset;
                     final float maxy = mMaxY;              
                     paint.setColor(0xFFAAAAAA);
                     cavas.drawColor(0xFFFFFFFF);
                     cavas.drawLine(xoffset, 0,      xoffset, maxy,      paint);  
                     cavas.drawLine(xoffset-70, 0,      xoffset-70, maxy,      paint);  
                     cavas.drawLine(xoffset+70, 0,      xoffset+70, maxy,      paint);  
                     cavas.drawLine(xoffset-140, 0,      xoffset-140, maxy,      paint);  
                     cavas.drawLine(xoffset+140, 0,      xoffset+140, maxy,      paint);  
                     cavas.drawLine(xoffset-210, 0,      xoffset-210, maxy,      paint);  
                     cavas.drawLine(xoffset+210, 0,      xoffset+210, maxy,      paint);  
                     
                     
                     if(avgDuration==0){
                    	 Paint p=new Paint();
                         p.setTextSize(20);
                         p.setColor(Color.RED);
                         String s=this.getContext().getResources().getString(R.string.tip);
                    	 cavas.drawText(s,0,  maxy-10, p);
                     }else{
                    	 Paint p=new Paint();
                         p.setTextSize(20);
                       //  String 
                         String s=this.getContext().getResources().getString(R.string.tip2, new Object[]{avgDuration,avg,avgColor});
                    	 cavas.drawText(s,0,  maxy-10, p);
                     }
                
                     if(listener!=null){
                    	 this.avgColor=listener.onCanvasReachEnd();
                     }
                 }              
                 canvas.drawBitmap(mBitmap, 0, 0, null);  
                 
             }
         }
     }	
	 
	 public void setAvg(float avg){
		 this.avg=avg;
	 }
	 
	 public void addCanvasFilledListener(CanvasFilledListener listener) {
		this.listener = listener;
	}
	
	public static interface CanvasFilledListener{
		 public int onCanvasReachEnd();
	 }
}
